package com.Endurancer.manager.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;


import com.Endurancer.manager.domain.dto.BlogDto;
import com.Endurancer.manager.domain.dto.BlogItemDto;
import com.Endurancer.manager.domain.dto.SearchDto;
import com.Endurancer.manager.domain.vo.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.Endurancer.common.annotation.Log;
import com.Endurancer.common.core.controller.BaseController;
import com.Endurancer.common.core.domain.AjaxResult;
import com.Endurancer.common.enums.BusinessType;
import com.Endurancer.manager.domain.BlogPost;
import com.Endurancer.manager.service.IBlogPostService;
import com.Endurancer.common.utils.poi.ExcelUtil;
import com.Endurancer.common.core.page.TableDataInfo;

/**
 * 博客详情信息Controller
 *
 * @author Endurancer
 * @date 2025-02-02
 */
@RestController
@RequestMapping("/manager/post")
public class BlogPostController extends BaseController
{
    @Autowired
    private IBlogPostService blogPostService;
    /**
     * 根据博客Id回显博客信息
     */
    @GetMapping("/selectMyBlog/{postId}")
    public AjaxResult selectMyBlog(@PathVariable("postId") Long postId)
    {

        BlogDto blogDto = blogPostService.selectMyBlog(postId);
        return success(blogDto);
    }
    /**
     * 发布博客
     */
    @PostMapping("/addBlog")
    public AjaxResult addBlog(@RequestBody BlogDto blogDto)
    {
        return toAjax(blogPostService.addBlog(blogDto));
    }
    /**
     * 修改博客
     */
    @PutMapping("/updateBlog")
    public AjaxResult updateBlog(@RequestBody BlogDto blogDto)
    {
        return toAjax(blogPostService.updateBlog(blogDto));
    }
    /**
     * 根据用户ID查询收藏博客列表
     */
    @GetMapping("/selectCollect/{userId}")
    public TableDataInfo selectCollect(@PathVariable("userId") Long userId)
    {
        startPage();
        List<MyBlogVo> list = blogPostService.selectCollect(userId);
        return getDataTable(list);
    }
    /**
     * 根据用户ID查询博客信息列表
     */
    @GetMapping("/selectByUserId/{userId}")
    public TableDataInfo selectByUserId(@PathVariable("userId") Long userId)
    {
        startPage();
        List<MyBlogVo> list = blogPostService.selectByUserId(userId);
        return getDataTable(list);
    }
    /**
     * 根据博客Id获取上一篇和下一篇
     */
    @GetMapping("/selectPageByPostId/{postId}")
    public AjaxResult selectBlogByKeyWord(@PathVariable("postId") Long postId) {
        BlogPageVo blogPageVo=blogPostService.selectPageByPostId(postId);
        return AjaxResult.success(blogPageVo);
    }

    /**
     * 根据关键字查询文章
     */
    @PostMapping("/selectBlogByKeyWord")
    public TableDataInfo selectBlogByKeyWord(@RequestBody SearchDto searchDto) {
        startPage();
        List<SearchVo> searchVo= blogPostService.selectBlogByKeyWord(searchDto);
        return getDataTable(searchVo);
    }
    /**
     * 博客标签文章主题数量
     */
    @GetMapping("/selectBlogCount")
    public AjaxResult selectBlogCount( )
    {
        BlogCountVo blogCountVo = blogPostService.selectBlogCount();
        return AjaxResult.success(blogCountVo);
    }

    /**
     * 根据标签Id查询博客信息列表
     */
    @GetMapping("/selectByTypeId/{typeId}")
    public TableDataInfo selectByTypeId(@PathVariable("typeId") Long typeId)
    {
        startPage();
        List<BlogVo> list = blogPostService.selectByTypeId(typeId);
        return getDataTable(list);
    }
    /**
     * 增加并查询博客浏览量
     */
    @GetMapping("/selectBlogViewsCount/{postId}")
    public AjaxResult selectBlogViewsCount(@PathVariable("postId") Long postId)
    {

        Long viewCount= blogPostService.selectBlogViewsCount(postId);
        return success(viewCount);
    }
    /**
     * 查询最新博客排行
     */
    @GetMapping("/selectByNewBlog")
    public AjaxResult selectByNewBlog()
    {

        List<BlogNewVo> list = blogPostService.selectByNewBlog();
        return success(list);
    }
    /**
     * 查询综合博客排行
     */
    @GetMapping("/selectByTopBlog")
    public AjaxResult selectByTopBlog()
    {

        List<BlogTopVo> list = blogPostService.selectByTopBlog();
        return success(list);
    }
    /**
     * 根据热度标签查询博客信息列表
     */
    @GetMapping("/selectByTagId/{tagId}")
    public TableDataInfo selectByTagId(@PathVariable("tagId") Long tagId)
    {
        startPage();
        List<ArticleVo> list = blogPostService.selectByTagId(tagId);
        return getDataTable(list);
    }

    /**
     * 根据主题标签查询博客信息列表
     */
    @GetMapping("/selectByThemeId/{themeId}")
    public TableDataInfo selectByThemeId(@PathVariable("themeId") Long themeId)
    {
        startPage();
        List<ArticleVo> list = blogPostService.selectByThemeId(themeId);
        return getDataTable(list);
    }

    /**
     * 查询博客点赞收藏信息列表
     */
    @PostMapping("/selectBlogItem")
    public AjaxResult selectBlogItem(@RequestBody BlogItemDto blogItemDto)
    {

        BlogItemVo blogItemVo= blogPostService.selectBlogItem(blogItemDto);
        return success(blogItemVo);
    }
    /**
     * 根据博客id查询博客标题信息列表
     */
    @GetMapping("/selectBlogTitle/{postId}")
    public AjaxResult selectBlogTitle(@PathVariable("postId") Long postId)
    {

        BlogTitleVo blogTitleVo= blogPostService.selectBlogTitle(postId);
        return success(blogTitleVo);
    }
    /**
     * 根据博客id查询博客信息列表
     */
    @GetMapping("/selectByPostId/{postId}")
    public AjaxResult selectByPostId(@PathVariable("postId") Long postId)
    {

        BlogDetailVo blogDetailVo= blogPostService.selectByPostId(postId);
        return success(blogDetailVo);
    }
    /**
     * 查询博客详情信息列表
     */
    @PreAuthorize("@ss.hasPermi('manager:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogPost blogPost)
    {
        startPage();
        List<BlogPost> list = blogPostService.selectBlogPostList(blogPost);
        return getDataTable(list);
    }
    /**
     * 根据时间查询博客列表
     */
    @GetMapping("/selectBlogArchives")
    public AjaxResult selectBlogArchives()
    {

        List<Map<String, Object>> list = blogPostService.selectBlogArchives();
        return success(list);
    }

    /**
     * 导出博客详情信息列表
     */
    @PreAuthorize("@ss.hasPermi('manager:post:export')")
    @Log(title = "博客详情信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogPost blogPost)
    {
        List<BlogPost> list = blogPostService.selectBlogPostList(blogPost);
        ExcelUtil<BlogPost> util = new ExcelUtil<BlogPost>(BlogPost.class);
        util.exportExcel(response, list, "博客详情信息数据");
    }

    /**
     * 获取博客详情信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(blogPostService.selectBlogPostByPostId(postId));
    }

    /**
     * 新增博客详情信息
     */
    @PreAuthorize("@ss.hasPermi('manager:post:add')")
    @Log(title = "博客详情信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogPost blogPost)
    {
        return toAjax(blogPostService.insertBlogPost(blogPost));
    }

    /**
     * 修改博客详情信息
     */
    @PreAuthorize("@ss.hasPermi('manager:post:edit')")
    @Log(title = "博客详情信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogPost blogPost)
    {
        return toAjax(blogPostService.updateBlogPost(blogPost));
    }

    /**
     * 删除博客详情信息
     */
    @Log(title = "博客详情信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(blogPostService.deleteBlogPostByPostIds(postIds));
    }
}
